package com.yanqu.road.server.manger.activity.zhenhuan;

import com.yanqu.road.dao.impl.activity.zhenhuan.ZhenHuanImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.zhenhuan.config.*;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.activity.zhenhuan.eZhenHuanChildType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanFirstProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanSecondProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanFirstProcessModule;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanSecondProcessModule;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanThirdProcessModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

public class ZhenHuanConfigMgr extends TempMgr {
    private static ZhenHuanFirstProcessConfig firstProcessConfig;

    private static ZhenHuanSecondProcessConfig secondProcessConfig;

    /**
     * 甄嬛三阶段
     */
    private static ZhenHuanThirdProcessConfig thirdProcessConfig;

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivity() {
        getLogger().info("ZhenHuanConfigMgr reload start.");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ZhenHuan.getValue());
        if (openActivityInfoList.isEmpty()) {
            firstProcessConfig = null;
            secondProcessConfig = null;
            thirdProcessConfig = null;
            getLogger().info("ZhenHuanConfigMgr not activity");
        } else {
            ZhenHuanFirstProcessConfig tempConfig1 = null;
            ZhenHuanSecondProcessConfig tempConfig2 = null;
            ZhenHuanThirdProcessConfig tempConfig3 = null;
            for (ActivityInfo tempActivityInfo : openActivityInfoList) {
                int activityId = tempActivityInfo.getActivityId();
                Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityId);
                if (tempActivityInfo.getChildType() == eZhenHuanChildType.First.getValue()) {
                    tempConfig1 = new ZhenHuanFirstProcessConfig(tempActivityInfo, activityConfigMap);
                } else if (tempActivityInfo.getChildType() == eZhenHuanChildType.Second.getValue()) {
                    tempConfig2 = new ZhenHuanSecondProcessConfig(tempActivityInfo, activityConfigMap);
                } else if (tempActivityInfo.getChildType() == eZhenHuanChildType.Third.getValue()) {
                    tempConfig3 = new ZhenHuanThirdProcessConfig(tempActivityInfo, activityConfigMap);
                } else {
                    getLogger().error("ZhenHuanConfigMgr reload error, activityInfo.getChildType() = {}", tempActivityInfo.getChildType());
                }
            }
            firstProcessConfig = tempConfig1;
            secondProcessConfig = tempConfig2;
            thirdProcessConfig = tempConfig3;
        }

        for (GamePlayer onlinePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            if (firstProcessConfig != null) {
                ZhenHuanFirstProcessModule module = onlinePlayer.getModule(ZhenHuanFirstProcessModule.class);
                module.iniSystem();
            }
            if (secondProcessConfig != null) {
                onlinePlayer.getModule(ZhenHuanSecondProcessModule.class).iniSystem();
            }

            if (thirdProcessConfig != null) {
                onlinePlayer.getModule(ZhenHuanThirdProcessModule.class).iniSystem();
            }
        }


        getLogger().info("ZhenHuanConfigMgr reload end.");
    }

    public static ZhenHuanFirstProcessConfig getFirstProcessConfig() {
        return firstProcessConfig;
    }

    public static ZhenHuanSecondProcessConfig getSecondProcessConfig() {
        return secondProcessConfig;
    }

    public static ZhenHuanThirdProcessConfig getThirdProcessConfig() {
        return thirdProcessConfig;
    }

    public static ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg getFirstProcessConfigMsg(String language) {
        if (firstProcessConfig == null){
            return null;
        }
        ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg configMsg = firstProcessConfig.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (firstProcessConfig){
                configMsg = firstProcessConfig.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = parseFirstProcessConfigMsg(language).build();
                    firstProcessConfig.setConfigMsg(language, configMsg);
                }
            }
        }
        return configMsg;
    }

    public static ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg.Builder parseFirstProcessConfigMsg(String language) {
        ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg.Builder builder = ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg.newBuilder();
        builder.setActivityId(firstProcessConfig.getActivityId());
        for (ZhenHuanStoryConfig storyConfig : firstProcessConfig.getStoryConfigMap().values()) {
            ZhenHuanProto.ZhenHuanFirstProcessConfigStoryTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanFirstProcessConfigStoryTemp.newBuilder();
            tempBuilder.setId(storyConfig.getId());
            tempBuilder.setName(ServerLanguageMgr.getContent(storyConfig.getName(), language));
            tempBuilder.setDesc(ServerLanguageMgr.getContent(storyConfig.getDesc(), language));
            tempBuilder.setConcubineComb(storyConfig.getConcubineComb());
            tempBuilder.setWeight(storyConfig.getWeight());
            tempBuilder.setReward(storyConfig.getReward());
            tempBuilder.setQuality(storyConfig.getQuality());
            tempBuilder.setFirstUnlockReward(storyConfig.getFirstUnlockReward());
            builder.addStoryList(tempBuilder);
        }

        for (ZhenHuanConcubineConfig concubineConfig : firstProcessConfig.getConcubineConfigMap().values()) {
            ZhenHuanProto.ZhenHuanFirstProcessConfigConcubineTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanFirstProcessConfigConcubineTemp.newBuilder();
            tempBuilder.setId(concubineConfig.getId());
            tempBuilder.setName(ServerLanguageMgr.getContent(concubineConfig.getName(), language));
            tempBuilder.setIcon(concubineConfig.getIcon());
            tempBuilder.setDesc(ServerLanguageMgr.getContent(concubineConfig.getDesc(), language));
            tempBuilder.setScore(concubineConfig.getScore());
            tempBuilder.setAttribute(concubineConfig.getAttribute());
            tempBuilder.setQuality(concubineConfig.getQuality());
            builder.addConcubineList(tempBuilder);
        }

        return builder;
    }

    public static boolean isZhenHuanPowerGoods(int goodsId) {
        if (firstProcessConfig != null && firstProcessConfig.getPowerGoodsId() == goodsId) {
            return true;
        }

        if (secondProcessConfig != null && secondProcessConfig.getPowerGoodsId() == goodsId) {
            return true;
        }

        return false;
    }

    public static ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg getSecondProcessConfigMsg(String language) {
        if (secondProcessConfig == null) {
            return null;
        }
        ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg configMsg = secondProcessConfig.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (secondProcessConfig) {
                configMsg = secondProcessConfig.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = parseSecondProcessConfigMsg(language);
                    secondProcessConfig.setConfigMsg(language, configMsg);
                }
            }
        }
        return configMsg;
    }

    public static ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg parseSecondProcessConfigMsg(String language) {
        ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg.Builder builder = ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg.newBuilder();
        builder.setActivityId(secondProcessConfig.getActivityId());

        for (ZhenHuanLevelConfig levelConfig : secondProcessConfig.getLevelConfigMap().values()) {
            ZhenHuanProto.ZhenHuanSecondProcessConfigLevelTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanSecondProcessConfigLevelTemp.newBuilder();
            tempBuilder.setId(levelConfig.getId());
            tempBuilder.setName(ServerLanguageMgr.getContent(levelConfig.getName(), language));
            tempBuilder.setLoveDegree(levelConfig.getLoveDegree());
            tempBuilder.setPrestigeDegree(levelConfig.getPrestigeDegree());
            tempBuilder.setOpenPartyPrestige(levelConfig.getOpenPartyPrestige());
            tempBuilder.setUseInvitationPrestige(levelConfig.getUseInvitationPrestige());
            tempBuilder.setSharePrestige(levelConfig.getSharePrestige());
            tempBuilder.setReward(levelConfig.getReward());
            tempBuilder.setPartyTime(levelConfig.getPartyTime());
            tempBuilder.setCompeteTime(levelConfig.getCompeteTime());
            builder.addLevelList(tempBuilder);
        }

        for (ZhenHuanPlaceConfig placeConfig : secondProcessConfig.getPlaceConfigMap().values()) {
            ZhenHuanProto.ZhenHuanSecondProcessConfigPlaceTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanSecondProcessConfigPlaceTemp.newBuilder();
            tempBuilder.setId(placeConfig.getId());
            tempBuilder.setName(ServerLanguageMgr.getContent(placeConfig.getName(), language));
            tempBuilder.setDesc(ServerLanguageMgr.getContent(placeConfig.getDesc(), language));
            tempBuilder.setProgress(placeConfig.getProgress());
            tempBuilder.setOverReward(placeConfig.getOverReward());
            tempBuilder.setCommonEvent(placeConfig.getCommonEvent());
            tempBuilder.setSpecialEvent(placeConfig.getSpecialEvent());
            tempBuilder.setReachReward(placeConfig.getReachReward());
            tempBuilder.setWeight(placeConfig.getWeight());
            tempBuilder.setLoveReward(placeConfig.getLoveReward());
            builder.addPlaceList(tempBuilder);
        }

        for (ZhenHuanTextConfig textConfig : secondProcessConfig.getTextConfigMap().values()) {
            ZhenHuanProto.ZhenHuanSecondProcessConfigTextTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanSecondProcessConfigTextTemp.newBuilder();
            tempBuilder.setId(textConfig.getId());
            tempBuilder.setDesc(ServerLanguageMgr.getContent(textConfig.getDesc(), language));
            builder.addTextList(tempBuilder);
        }

        for (ZhenHuanNpcConfig npcConfig : secondProcessConfig.getNpcConfigMap().values()) {
            ZhenHuanProto.ZhenHuanSecondProcessConfigNpcTemp.Builder tempBuilder = ZhenHuanProto.ZhenHuanSecondProcessConfigNpcTemp.newBuilder();
            tempBuilder.setId(npcConfig.getId());
            tempBuilder.setName(ServerLanguageMgr.getContent(npcConfig.getName(), language));
            tempBuilder.setIcon(npcConfig.getIcon());
            tempBuilder.setLevel(npcConfig.getLevel());
            builder.addNpcList(tempBuilder);
        }

        return builder.build();
    }
}
